<?php
require('config.php');

        $user_id=$_REQUEST['user_id'];
        $yinhang=$_REQUEST['yinhang'];
        $zhihang=$_REQUEST['zhihang'];
        $kahao=$_REQUEST['kahao'];
        $xingming=$_REQUEST['xingming'];
        $sq_money=$_REQUEST['sq_money'];
        $tel=$_REQUEST['tel'];
        $time=time();
		$sql="select * from `".$oto."_users` where `userId`='{$user_id}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_sys_configs` where `fieldCode`='applyMin' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$sys[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_apply` where `userId`='$user_id' and `status`='0' ";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$apply[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($sys))
		{
			if($sq_money<$sys[0]['fieldValue'])
			{
				$data[0]['pan']=-1;
				$data[0]['msg']="申请金额不能小于".$sys[0]['fieldValue'];
				echo json_encode($data);
				exit();
			}
		}
		if($user_info[0]['userMoney']<$sq_money)
		{
			if($sq_money<$sys[0]['fieldValue'])
			{
				$data[0]['pan']=-1;
				$data[0]['msg']="申请金额不能大于当前余额";
				echo json_encode($data);
				exit();
			}
		}
		if(isset($apply))
		{
			$data[0]['pan']=-1;
			$data[0]['msg']="申请失败：已有申请审核中";
			echo json_encode($data);
			exit();
		}
		$z_money=$user_info[0]['userMoney']-$sq_money;
        $sql="update ".$oto."_users set userMoney='{$z_money}' where userId='{$user_id}'";
		$result=$db->query($sql);
		if($result)
		{
			$sql="INSERT INTO ".$oto."_apply (`userId`,`applyType`,`tel`,`applyPrice`,`bankUserName`,`bankName`,`bankAccess`,`status`,`bankNum`,`time`) VALUES ('$user_id','0','$tel','$sq_money','$xingming','$yinhang','$zhihang','0','$kahao','$time')";
	        $result=$db->query($sql);
	        $sql="select * from `".$oto."_apply` where `userId`='$user_id' and `time`='$time' and `bankNum`='$kahao'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$apply2[]=$row;//将取得的所有数据赋值给person_info数组
			}
	        
	        if(isset($apply2)) $order_no=$apply2[0]['id'];
	        else $order_no='0';
	        $sql="INSERT INTO ".$oto."_money_record (`userid`,`orderNo`,`money`,`balance`,`time`,`IncDec`,`type`,`remark`,`payWay`) VALUES ('$user_id','$order_no','$sq_money','$z_money','$time','0','4','个人提现','3')";
	        //file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
	        $result=$db->query($sql);
	        $data[0]['pan']=1;
			$data[0]['msg']="申请成功";
		}
		else
		{
			$data[0]['pan']=-1;
			$data[0]['msg']="申请失败";
		}
		echo json_encode($data);
?>